"""
@author: dfy
@file: 13.剪刀石头布.py
@time: 2024/8/23 14:33
"""
import random


# 可以创建游戏对象
# 游戏对象可以加入到房间
# 同一个房间内的两个游戏对象可以进行游戏（一个房间最多两个人）
# 每次两个游戏对象随机的出剪刀石头布
# 三局两胜，按任意键开始

# 玩家类
#  属性：姓名、房间
#  方法：战斗

# 房间类
# 属性：房间号、房间成员
# 方法：加入玩家
class Player:
    def  __init__(self,name,room = None):
        self.name = name
        self.room = room
        self.num = None
        self.flood = 3
        print("创建角色成功")

    def fight(self,other):
        round = 1
        while self.flood >0 and other.flood >0:
            print(f"第{round}回合：")
            self.num =  random.choice([1,2,3])
            print(f"{self.name}出{self.num}")
            other.num = random.choice([1,2,3])
            print(f"{other.name}出{other.num}")
            if self.num - other.num == 0:
                self.flood -= 1
                other.flood -= 1
                print("打平，双方都扣一滴血")
            elif self.num - other.num == -1 or self.num - other.num == 2:
                self.flood -= 1
                print(f"{self.name}扣一滴血")
            elif self.num - other.num == 1 or self.num - other.num == -2:
                other.flood -= 1
                print(f"{other.name}扣一滴血")
            round += 1

        if self.flood > 0:
            print(f"{self.name}胜出")
        else:
            print(f"{other.name}胜出")





class Room:
    def __init__(self,room_id):
        self.room_id = room_id
        self.room_members = []
        print("创建房间成功")

    def add_player(self,player):
        if len(self.room_members) == 2:
            print("房间成员已满！")
        elif self and player:
            player.room = self.room_id
            self.room_members.append(player)
            if len(self.room_members) == 2:
                print("进入战斗")
                self.room_members[0].fight(self.room_members[1])




        # player.room = self
        # self.room_members.append(player.name)



# 无限输入：
# 1、创建房间
# 2、创建角色
# 3、将角色加入房间
# 4、选择房间战斗
# 5、退出


def ListPlayer(players):
    for k,v in players.items():
        print(f"Name:{v.name},Room:{v.room}")

def ListRoom(rooms):
    for k,v in rooms.items():
        print(f"Room:{v.room_id},Member:{v.room_members}")

players = {}
rooms = {}
while True:
    try:
        print("1、创建房间\n2、创建角色\n3、将角色加入房间\n4、选择战斗房间\n5、退出")
        init = int(input("请输入你的选择:"))
        if init == 1:
            room_id = input("请输入你要创建的房间号：")
            room = Room(room_id)
            rooms[room_id] = room
            print("现在已有的房间".center(20,"*"))
            ListRoom(rooms)
        elif init == 2:
            name = input("请输入你想创建的角色名称：")
            player = Player(name)
            players[name] = player
            print("现在已有的角色".center(20,"*"))
            ListPlayer(players)

        elif init == 3:
            print("现在已有的房间")
            ListRoom(rooms)
            print("现在已有的角色")
            ListPlayer(players)
            add_player_name = input("请输入你想加入的角色：")
            add_room_id = input("请输入你想加入的房间：")
            target_player = None
            target_room = None
            try:
                if add_player_name in players:
                    target_player = players[add_player_name]

                if add_room_id in rooms:
                    target_room = rooms[add_room_id]

                target_room.add_player(target_player)
            except:
                print("加入失败，角色或者房间不存在")

        elif init ==4:
            print("现在已有的房间")
            ListRoom(rooms)
            print("现在已有的角色")
            ListPlayer(players)
        elif init == 5:
            break
    except:
        print("非法输入，请重新选择")